/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.maslyk.invitation.service;

import java.security.NoSuchAlgorithmException;

/**
 *
 * @author kris
 */
public class MD5Service
{
    public static String generateHexString (String passwd)
    {
        java.security.MessageDigest md;

        try
        {
            md = java.security.MessageDigest.getInstance("MD5");
        }
        catch (NoSuchAlgorithmException e)
        {
            throw new IllegalArgumentException(e);
        }

        byte[] b_passwd = passwd.getBytes(); //uses a DEFAULT encoding!!
        byte[] b_passwd_encoded = md.digest(b_passwd);

        // Turn it into a hex-escaped MD5 string like PHP md5() function
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < b_passwd_encoded.length; ++i)
            sb.append(Integer.toHexString((b_passwd_encoded[i] & 0xFF) | 0x100).toLowerCase().substring(1,3));

        return sb.toString();
    }
}